package com.zis.platform.controller;

import com.zis.core.SearchParams;
import com.zis.platform.model.User;
import com.zis.platform.service.IUserInter;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Create by wangshen 2017/12/21
 */
@RestController
@RequestMapping("/api/sys/user")
public class UserController {

    @Autowired
    private IUserInter userInter;

    /**
     * 分页查询
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public PageQuery<User> findByPage(@RequestBody SearchParams params) throws Exception {
        return userInter.findByPage(params);
    }

    /**
     * 用户保存
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(HttpServletRequest request, @RequestBody User user) throws Exception {
        user.setCreateUser(request.getAttribute("code").toString());
        return userInter.insert(user);
    }

    /**
     * 更新
     * @param request
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(HttpServletRequest request, @RequestBody User user) throws Exception {
        user.setUpdateUser(request.getAttribute("code").toString());
        return userInter.update(user);
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable("id") String id) throws Exception{
        return userInter.delete(id);
    }
}
